package com.centriole.service.exuser.dao;

import com.centriole.common.entity.ExRole;
import com.centriole.common.entity.ExUser;
import com.centriole.common.entity.ExUserLoginStrategyEntity;
import com.centriole.common.entity.ExUserTokenEntity;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;

import java.util.List;

/**
 *  功   能： 对外使用ExUserMapper
 *  创建人：陈默晗
 *  时   间：2018年4月10日上午11:11:28
 */
@Mapper
public interface ExUserMapper {

    /**
     * 新增用户
     * @param exUser
     * @return
     */
    @Insert("INSERT INTO ex_user(userId,accountName,password,nickName,status,lastLogin) VALUES(#{u.userId},#{u.accountName},#{u.password},#{u.nickName},1,now() )")
    @SelectKey(statement="select replace(UUID(),'-','') as userId", keyProperty="u.userId", before=true, statementType= StatementType.STATEMENT,resultType=String.class)
    int insertExUser(@Param("u") ExUser exUser);

    /**
     * 全查询有效用户
     * @return  ExUser
     */
    @Select("SELECT * FROM ex_user WHERE status = 1")
    List<ExUser> selectAll();

    /*
     * 根据 账号和密码查找对应的用户
     * @return  ExUser
     */
    @Select("SELECT * FROM ex_user WHERE accountName = #{accountName} AND password = #{password}")
    ExUser selectExUserByAccountAndPassword(@Param("accountName") String accountName , @Param("password") String password);

    /**
     * 指定userId查询用户
     * @param userId
     * @return
     */
    @Select("SELECT * FROM ex_user WHERE userId = #{userId}")
    ExUser selectByUserId(String userId);

    /**
     * 指定accountName查询用户
     * @param accountName
     * @return
     */
    @Select("SELECT * FROM ex_user WHERE accountName = #{accountName}")
    ExUser selectByAccountName(String accountName);

    /**
     * 指定permissionId查询用户
     * @param roleId
     * @return
     */
    @Select("SELECT  `user`.* FROM ex_user `user` " +
            "LEFT JOIN ex_user_role ON `user`.userId = ex_user_role.userId  " +
            "LEFT JOIN ex_role `role` ON `role`.roleId = ex_user_role.roleId " +
            "WHERE  `role`.roleId = #{rid}")
    List<ExUser> selectByRoleId(@Param("rid") String roleId);

    /**
     * 指定roleName查询用户
     * @param roleName
     * @return
     */
    @Select("SELECT  `user`.* FROM ex_user `user` " +
            "LEFT JOIN ex_user_role ON `user`.userId = ex_user_role.userId  " +
            "LEFT JOIN ex_role `role` ON `role`.roleId = ex_user_role.roleId " +
            "WHERE  `role`.roleName = #{rName}")
    List<ExUser> selectByRoleName(@Param("rName") String roleName);

    /**
     * 指定permissionId查询用户
     * @param permissionId
     * @return
     */
    @Select("SELECT  `user`.* FROM ex_user `user` " +
            "LEFT JOIN ex_user_role ON `user`.userId = ex_user_role.userId  " +
            "LEFT JOIN ex_role `role` ON `role`.roleId = ex_user_role.roleId " +
            "LEFT JOIN ex_role_permission ON `role`.roleId = ex_role_permission.roleId " +
            "LEFT JOIN ex_permission `permission` ON `permission`.permissionId = ex_role_permission.permissionId " +
            "WHERE  `permission`.permissionId = #{pid}")
    List<ExUser> selectByPermissionId(@Param("pid") String permissionId);

    /**
     * 指定permissionName查询用户
     * @param permissionName
     * @return
     */
    @Select("SELECT  `user`.* FROM ex_user `user`  " +
            "LEFT JOIN ex_user_role ON `user`.userId = ex_user_role.userId  " +
            "LEFT JOIN ex_role `role` ON `role`.roleId = ex_user_role.roleId  " +
            "LEFT JOIN ex_role_permission ON `role`.roleId = ex_role_permission.roleId " +
            "LEFT JOIN ex_permission `permission` ON `permission`.permissionId = ex_role_permission.permissionId " +
            "WHERE  `permission`.permissionName = #{pName}")
    List<ExUser> selectByPermissionName(@Param("pName") String permissionName);

    /**
     * 给指定用户关联一个角色
     * @param userId
     * @param roleId
     * @return
     */
    @Insert("INSERT INTO ex_user_role(userId,roleId) VALUES (#{userId},#{roleId})")
    int relateUserAndRole(@Param("userId") String userId, @Param("roleId") String roleId);

    /**
     * 给指定角色关联一个关联
     * @param roleId
     * @param permissionId
     * @return
     */
    @Insert("INSERT INTO ex_role_permission(roleId,permissionId) VALUES (#{roleId},#{permissionId})")
    int relateRoleAndPermission(@Param("roleId") String roleId, @Param("permissionId") String permissionId);

    /**
     * 关联查询所有角色(包含对应的权限)
     * @return
     */
    List<ExRole> selectRAllRoles();

    /**
     * 指定RoleName查询角色(包含对应的权限)
     * @return
     */
    ExRole selectRRolesByRoleName(@Param("roleName") String roleName);

    /**
     * 关联全查询用户(包含用户的角色和权限)
     * @return
     */
    List<ExUser> selectRAll();

    /**
     * 指定userId查询用户(包含用户的角色和权限)
     * @return
     */
    ExUser selectRByUserId(@Param("userId") String userId);

    /**
     * 修改 用户实体
     * @param exUser
     * @return
     */
    int update(@Param("u") ExUser exUser);

    @Update("UPDATE ex_user SET token ='' , modifierId= #{userId}, modifyTime = now() WHERE ex_user.userId = #{userId}")
    int logout(@Param("userId") String userId);

    /**
     * 向 ex_user_token 表中 插入一条记录
     * @param token ExUserTokenEntity
     * @return
     */
    @Insert("INSERT INTO ex_user_token(userId , ip , token , creatorId , modifierId , tokenTime) VALUES (#{t.userId} , #{t.ip} , #{t.token} , #{t.userId} , #{t.userId} , now() )")
    int  insertToken(@Param("t") ExUserTokenEntity token);

    /**
     * 刷新指定用户的token值
     * @param userId    指定的用户id
     * @param token     字符串签名
     * @return
     */
    @Update("UPDATE  ex_user_token SET token = #{token} , modifierId = #{userId} , modifyTime = now() , tokenTime = now()  WHERE userId = (select userId from ex_user where accountName=#{userId}) ")
    int refershToken(@Param("userId")String userId , @Param("token")String token );

    /**
     * 指定userId 查询 该用户的 登录策略实体
     * @param userId    32个字符串的userId
     * @return  ExUserLoginStrategyEntity
     */
    @Select("SELECT * FROM ex_user_login_strategy WHERE userId = #{userId}")
    ExUserLoginStrategyEntity selectByStrategy(@Param("userId") String userId);

    /**
     * 向 登录策略表 添加一条记录
     * @param strategy  ExUserLoginStrategyEntity
     * @return
     */
    @Insert("INSERT INTO ex_user_login_strategy(userId,loginWay,tokenAging,upperLimit,loginCount,creatorId,modifierId) VALUES (#{s.userId},#{s.loginWay},#{s.tokenAging},#{s.upperLimit},#{s.loginCount},#{s.userId},#{s.userId})")
    int insertLoginStrategy(@Param("s") ExUserLoginStrategyEntity strategy);

    @Update("UPDATE ex_user_login_strategy SET loginCount = loginCount+1 , modifierId=#{userId} , modifyTime = now() WHERE userId = #{userId}")
    int addLoginCount(@Param("userId") String userId);

}
